/**
 * MemberController.java 上午10:41:19 2012-2-29
 * Cyber Information Technology Co.,Ltd.
 *   
 *   http://www.citchina.cn
 *   citchina.org@gmail.com
 */
package cn.cit.wcif.member.web;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.cit.wcif.common.web.AbstractController;
import cn.cit.wcif.member.api.IMemberService;
import cn.cit.wcif.member.api.exceptions.KeyNotUniqueException;
import cn.cit.wcif.member.api.exceptions.MemberNonExistenceException;
import cn.cit.wcif.member.api.exceptions.MemberRegistException;
import cn.cit.wcif.member.web.model.MemberModel;

/**
 * @author Jonathan
 * @version 1.0
 * 
 */
@Controller
@RequestMapping("/member")
public class MemberController extends AbstractController {
	@Autowired
	@Qualifier("memberServiceImpl")
	IMemberService memberService;

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_INDEX, method = RequestMethod.GET)
	public String index() {
		return NamingConventions.URL.VIEW_PATH_INDEX;
	}

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_HOME, method = RequestMethod.GET)
	public String home() {
		return NamingConventions.URL.VIEW_PATH_HOME;
	}

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_REGIST, method = RequestMethod.GET)
	public String regist() {
		return NamingConventions.URL.VIEW_PATH_REGIST;
	}

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_LOGINFAIL, method = RequestMethod.GET)
	public String loginFail() {
		return NamingConventions.URL.VIEW_PATH_LOGINFAIL;
	}

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_REGISTMEMBER, method = RequestMethod.GET)
	@ResponseBody
	public String registMember(
			@ModelAttribute("memberModel") MemberModel memberModel,
			ModelMap model) throws UnsupportedEncodingException {
		String name = memberModel.getName();
		if (null == name && "".equals(name)) {
			try {
				memberService.registMember(memberModel.getEmail(),
						memberModel.getPassword());
			} catch (KeyNotUniqueException e) {
				return "false";
			} catch (MemberRegistException e) {
				return "false";
			}
		} else {
			try {
				memberService.registMember(memberModel.getEmail(),
						memberModel.getPassword(), decodeParamValue(name));
			} catch (KeyNotUniqueException e) {
				return "false";
			} catch (MemberRegistException e) {
				return "false";
			}
		}
		return "true";
	}

	@RequestMapping(value = NamingConventions.URL.ACTION_PATH_LOGIN, method = RequestMethod.GET)
	@ResponseBody
	public String login(@ModelAttribute("memberModel") MemberModel memberModel,
			ModelMap model) throws UnsupportedEncodingException {

		try {
			memberService.login(memberModel.getEmail(),
					memberModel.getPassword());
		} catch (MemberNonExistenceException e) {
			return "false";
		}
		return "true";
	}
}
